<?php
class Receptionist_Controller extends AdminController {

    public $REGISTRATION_LIST_UUID = 'admin/seminarAccounts/registrationList';
    
    public function __construct() {
        parent::__construct('person');
        $this->setDefaultListOrder('id');
        $this->pagination->enabled = false;
    }
    
    public function listing() {
    	$seminar = $this->getSeminar();

    	$this->viewData['receptionistList'] = $this->dao->queryAll($this->orderLimitAndOffset(
    			'SELECT p.*, a.login ' .
    			'FROM receptionist rec ' .
    			'JOIN person p ON rec.person_id = p.id ' .
    			'LEFT OUTER JOIN account a ON p.id = a.person_id'));
        
    	$this->viewData['personAcValues'] = $this->dao->queryOptions(
    			'SELECT p.id, p.first_name, p.last_name, YEAR(p.birth_date) ' .
    			'FROM person p ' .
                'WHERE p.id NOT IN (SELECT person_id FROM receptionist WHERE seminar_id = :seminar_id) ',
                array('seminar_id' => $seminar['id']),
                array(' ', '()'));
    	$this->viewData['formData'] = array('seminar_id' => $seminar['id']);
    	
        $this->renderSubview('admin/seminar/seminarReceptionistList');
    }
    
    private function getSeminar() {
    	$seminarId = $this->input->get('seminar_id');
    	if (! $seminarId) {
    		$seminarId = $this->input->post('seminar_id');
    	}
    	if (! valid::isId($seminarId)) {
    		$this->redirectToList('error-invalidId');
    	}
    	
    	$seminar = $this->dao->fetchSeminar($seminarId, $this->getL10nLanguageCode());
    	if (! $seminar) {
    		$this->redirectToList('error-recordNotFound');
    	}
    	$this->viewData['seminar'] = $seminar;
    	return $seminar;
    }
    
    private function getPerson($seminar) {
    	$personId = $this->input->post('person_id');
    	if (! $personId) {
    		$personId = $this->input->get('person_id');
    	}
    	 
    	if (! valid::isId($personId)) {
    		$this->msg->error('error-invalidId');
    		$this->redirect('/admin/receptionist/listing?seminar_id=' . $seminar['id']);
    	}
    	$person = $this->dao->getById('person', $personId);
    	if (! $person) {
    		$this->msg->error('error-recordNotFound');
    		$this->redirect('/admin/receptionist/listing?seminar_id=' . $seminar['id']);
    	}
    	return $person;
    }
    
    protected function redirectToList($errorKey=NULL) {
        if ($errorKey) {
            $this->msg->error($errorKey);
        }
        $this->redirect('/admin/seminar/all');
    }
    
    public function create() {
    	$seminar = $this->getSeminar();
    	$person = $this->getPerson($seminar);
    	$receptionist = array(
    			'person_id' => $person['id'],
    			'seminar_id' => $seminar['id']);
    	$this->dao->insert('receptionist', $receptionist, null, false, true, false);
    	$this->msg->info('receptionist-createSuccess', format::personFullName($person));
    	$this->redirect('/admin/receptionist/listing?seminar_id=' . $seminar['id']);
    }
    
    public function delete() {
    	$seminar = $this->getSeminar();
    	$person = $this->getPerson($seminar);
    	$receptionistPk = array(
    			'person_id' => $person['id'],
    			'seminar_id' => $seminar['id']);
    	$this->dao->delete('receptionist', $receptionistPk);
    	
    	$this->msg->info('receptionist-deleteSuccess', format::personFullName($person));
    	$this->redirect('/admin/receptionist/listing?seminar_id=' . $seminar['id']);
    }
}
